const Core = require('@alicloud/pop-core');
const jwt = require("jsonwebtoken");
const tencentcloud = require("tencentcloud-sdk-nodejs");


module.exports = {
    // 腾讯云
    sendMsg(tel, code) {
        // Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
        const SmsClient = tencentcloud.sms.v20190711.Client;
        const clientConfig = {
            credential: {
                secretId: "AKID5oLbaPUAzKWYSCrZJyYvW0GOsChLi8LZ",
                secretKey: "KgeNq2MPziBdopgV9PVRQtBd2iRya6MT",
            },
            region: "",
            profile: {
                httpProfile: {
                    endpoint: "sms.tencentcloudapi.com",
                },
            },
        };
        const client = new SmsClient(clientConfig);
        const params = {
            "PhoneNumberSet": [
                "+86" + tel
            ],
            "TemplateID": "881527",
            "Sign": "爱code爱生活",
            "TemplateParamSet": [
                code
            ],
            "SmsSdkAppid": "1400489828"
        };
        return new Promise((resolve, reject) => {
            client.SendSms(params).then(
                (data) => {
                    resolve(JSON.stringify(data));
                },
                (err) => {
                    reject("error", err);
                }
            );
        })
    },
    // 阿里云
    // sendMsg(tel, code) {
    //     var client = new Core({
    //         accessKeyId: 'LTAI4GHsvUyWBB9YBMfHbHk6',
    //         accessKeySecret: 'Om5GaHNITnjphhehT0lh4uL5ASnXJ2',
    //         endpoint: 'https://dysmsapi.aliyuncs.com',
    //         apiVersion: '2017-05-25'
    //     });
    //     var params = {
    //         "RegionId": "cn-hangzhou",
    //         "PhoneNumbers": tel,
    //         "SignName": "爱code爱生活",
    //         "TemplateCode": "SMS_203728335",
    //         "TemplateParam": `{"code":${code}}`
    //     }
    //     var requestOption = {
    //         method: 'POST'
    //     };

    //     return new Promise((resolve,reject)=>{
    //         client.request('SendSms', params, requestOption).then((result) => {
    //             resolve(JSON.stringify(result));
    //         }, (ex) => {
    //             reject(ex);
    //         })
    //     })
    // },
    verify(token){
        return new Promise((resolve, reject)=>{
            jwt.verify(token, "横扫千军", (err, data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    }
}



// 执行方式
// tools.sendMsg(19939713202, 999999).then((result)=>{

// }).catch((err)=>{

// })